﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using HiLand.Utility.Setting;
using HiLand.Utility.Web;
using GBFinance.Entity;
using GBFinance.BLL;
using HiLand.General.Entity;
using HiLand.General.BLL;

namespace GBFinance.Web.Models
{
    public class Miscs
    {
        private static IFormatProvider currentCultureInfo = null;
        /// <summary>
        /// 系统使用的文化信息
        /// </summary>
        public static IFormatProvider CurrentCultureInfo
        {
            get
            {
                if (currentCultureInfo == null)
                {
                    string usingCultureInfoString = Config.GetAppSetting("usingCultureInfo");
                    if (string.IsNullOrWhiteSpace(usingCultureInfoString))
                    {
                        usingCultureInfoString = "en-AU";
                    }

                    currentCultureInfo = new CultureInfo(usingCultureInfoString);
                }

                return currentCultureInfo;
            }
        }

        private static string dateTimeFormating = string.Empty;
        /// <summary>
        /// 系统使用的日期格式
        /// </summary>
        public static string DateTimeFormating
        {
            get
            {
                if (dateTimeFormating == string.Empty)
                {
                    dateTimeFormating = Config.GetAppSetting("dateTimeFormating");
                }

                if (string.IsNullOrWhiteSpace(dateTimeFormating))
                {
                    dateTimeFormating = "dd/MM/yyyy";
                }

                return dateTimeFormating;
            }
        }

        public static LoanSimpleInfo CurrentSecuredLoanInfo
        {
            get { return CurrentLoanInfo(true); }
        }

        public static LoanSimpleInfo CurrentUnSecuredLoanInfo
        {
            get { return CurrentLoanInfo(false); }
        }

        #region 常用配置
        private static int pageSizeManage = 0;
        /// <summary>
        /// 后台管理中信息列表时，每页显示的信息数
        /// </summary>
        public static int PageSizeManage
        {
            get
            {
                if (pageSizeManage == 0)
                {
                    pageSizeManage = Config.GetAppSettingInt("PageSizeManage",10);
                }
                return pageSizeManage;
            }
        }

        private static int pageSizeFront = 0;
        /// <summary>
        /// 后台管理中信息列表时，每页显示的信息数
        /// </summary>
        public static int PageSizeFront
        {
            get
            {
                if (pageSizeFront == 0)
                {
                    pageSizeFront = Config.GetAppSettingInt("PageSizeFront",10);
                }
                return pageSizeFront;
            }
        }
        #endregion

        private static LoanSimpleInfo CurrentLoanInfo(bool isSecuredLoad)
        {

            Guid loanGuid = Guid.Empty;
            Guid enterpriseGuid = Guid.Empty;
            //1.通过请求参数获取信息（主要用于后台管理）
            string adminLoanGuid = RequestHelper.GetValue("adminLoanGuid");
            if (string.IsNullOrWhiteSpace(adminLoanGuid) == false && adminLoanGuid != Guid.Empty.ToString())
            {
                loanGuid = new Guid(adminLoanGuid);
            }

            string adminEnterpriseGuid = RequestHelper.GetValue("adminEnterpriseGuid");
            if (string.IsNullOrWhiteSpace(adminEnterpriseGuid) == false && adminEnterpriseGuid != Guid.Empty.ToString())
            {
                enterpriseGuid = new Guid(adminEnterpriseGuid);
            }
            else
            {
                if (loanGuid != Guid.Empty)
                {
                    LoanBasicEntity loanEntity = LoanBasicBLL.Get(loanGuid);
                    if (loanEntity != null)
                    {
                        enterpriseGuid = loanEntity.LoanUserID;
                    }
                }
            }


            //2.通过Cookie获取信息（主要用于客户的贷款流程）
            LoanCookie loanCookie = null;
            if (isSecuredLoad)
            {
                loanCookie = CookieInfo.Load<SecuredLoanCookie>();
            }
            else
            {
                loanCookie = CookieInfo.Load<UnSecuredLoanCookie>();
            }

            string enterpriseGuidString = loanCookie.EnterpriseGuid;
            string loandGuidString = loanCookie.LoanGuid;

            if (enterpriseGuid == Guid.Empty && string.IsNullOrWhiteSpace(enterpriseGuidString) == false && enterpriseGuidString != Guid.Empty.ToString())
            {
                enterpriseGuid = new Guid(enterpriseGuidString);
            }

            if (loanGuid == Guid.Empty && string.IsNullOrWhiteSpace(loandGuidString) == false && loandGuidString != Guid.Empty.ToString())
            {
                loanGuid = new Guid(loandGuidString);
            }

            return new LoanSimpleInfo(loanGuid, enterpriseGuid);

        }

        /// <summary>
        /// 关于贷款的最概要的信息
        /// </summary>
        public class LoanSimpleInfo
        {
            public LoanSimpleInfo(Guid loanGuid, Guid enterpriseGuid)
            {
                this.LoanGuid = loanGuid;
                this.EnterpriseGuid = enterpriseGuid;
            }

            public Guid LoanGuid { get; set; }
            public Guid EnterpriseGuid { get; set; }
        }
    }
}